package com.wz.adapter;

import java.util.List;

import com.wz.activity.R;
import com.wz.bean.ContactBean;

import android.annotation.SuppressLint;
import android.content.Context;

import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ContactExpAdapter extends BaseExpandableListAdapter{
	
	private Context context;
	private List<String> group;
	private List<List<ContactBean>> childsData;
	public ContactExpAdapter(Context context,List<String> group,List<List<ContactBean>> childsData){
		this.context = context;
		this.group = group;
		this.childsData = childsData;
	}
	
	int[] logos = new int[]{
			R.drawable.cat
	};
	
	
	@Override
	public int getGroupCount() {
		//Log.d("Tag", "getGroupCount ..."+group.size());
		return group.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		//Log.d("Tag", "getChildrenCount ..."+childsData.get(groupPosition).size());
		return childsData.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return group.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		
		return childsData.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {

		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}
	
	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
		
		LinearLayout ll = new LinearLayout(context);
		ll.setOrientation(android.widget.LinearLayout.HORIZONTAL);
		ll.setGravity(Gravity.CENTER);
		
		/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(38,38);
		ImageView logo = new ImageView(context);
		logo.setLayoutParams(lp);
		logo.setImageResource(logos[0]);
		
		ll.addView(logo);*/
		
		TextView textView = getTextView();
		textView.setText(getGroup(groupPosition).toString());
		ll.addView(textView);
		
		return ll;
	}

	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		
		 ContactBean contactBean = childsData.get(groupPosition).get(childPosition);
		String name = contactBean.getName();
		String phoneNum = contactBean.getPhoneNum();
		LinearLayout ly = getLinearLayout(name,phoneNum);
		
		return ly;
	}

	@SuppressLint("RtlHardcoded")
	private LinearLayout getLinearLayout(String name,String phone){
		LinearLayout ll = new LinearLayout(context);
		ll.setOrientation(android.widget.LinearLayout.VERTICAL);
		
		LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
		TextView nameView = new TextView(context);
		nameView.setLayoutParams(lp);
		nameView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		nameView.setPadding(36, 0,0,0);
		nameView.setTextSize(18);
		nameView.setText(name);
		
		ll.addView(nameView);
		
		LinearLayout.LayoutParams lpPhone = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
		TextView phoneView = new TextView(context);
		phoneView.setLayoutParams(lpPhone);
		phoneView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
		phoneView.setPadding(36, 0,0,0);
		phoneView.setTextSize(14);
		phoneView.setText(phone);
	
		ll.addView(phoneView);
		
		return ll;
	}
	
	@SuppressLint("RtlHardcoded")
	private TextView getTextView(){
		AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,64);
		TextView textView = new TextView(context);
		textView.setLayoutParams(lp);
		textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
		textView.setPadding(0, 0,10,0);
		textView.setTextSize(20);
		
		return textView;
	}

}
